using UnityEngine;
using System.Collections;


public static class RectangleExtensions
{
    /// <summary>
    /// Calculates the signed depth of intersection between two rectangles.
    /// </summary>
    /// <returns>
    /// The amount of overlap between two intersecting rectangles. These
    /// depth values can be negative depending on which wides the rectangles
    /// intersect. This allows callers to determine the correct direction
    /// to push objects in order to resolve collisions.
    /// If the rectangles are not intersecting, Vector2.Zero is returned.
    /// </returns>
    
	public static Vector2 GetIntersectionDepth(this Rectangle rectA, Rectangle rectB)
    {
	//Debug.LogError("rectA = " + rectA.ToString() + "rectB = " + rectB.ToString());
            
	// Calculate half sizes.
        float halfWidthA = rectA.Width / 2.0f;
        float halfHeightA = rectA.Height / 2.0f;
        float halfWidthB = rectB.Width / 2.0f;
        float halfHeightB = rectB.Height / 2.0f;

            // Calculate centers.
        Vector2 centerA = new Vector2(rectA.Left + halfWidthA, rectA.Top - halfHeightA);
        Vector2 centerB = new Vector2(rectB.Left + halfWidthB, rectB.Top - halfHeightB);
		
		//Debug.LogError("CenterA = " + centerA.ToString() + "CenterB = " + centerB.ToString());
		
        // Calculate current and minimum-non-intersecting distances between centers.
        float distanceX = centerA.x - centerB.x;
        float distanceY = centerA.y - centerB.y;
        float minDistanceX = halfWidthA + halfWidthB;
        float minDistanceY = halfHeightA + halfHeightB;

        // If we are not intersecting at all, return (0, 0).
        if (Mathf.Abs(distanceX) >= minDistanceX || Mathf.Abs(distanceY) >= minDistanceY)
            return Vector2.zero;

        // Calculate and return intersection depths.
        float depthX = distanceX > 0 ? minDistanceX - distanceX : -minDistanceX - distanceX;
        float depthY = distanceY > 0 ? minDistanceY - distanceY : -minDistanceY - distanceY;
        return new Vector2(depthX, depthY);
    }
	
	/*public static Vector2 GetIntersectionDepthNew(Rectangle targetRectangle, Vector2 lastPosition, Vector2 recentPosition)
    {
		Segment movementVector2 = new Segment(lastPosition, recentPosition);
		
		bool collideWithLeftBorder = CollisionHelper.CheckTwoSegmentCrose(movementVector2, targetRectangle.LeftBorder);
		bool collideWithBottomBorder = CollisionHelper.CheckTwoSegmentCrose(movementVector2, targetRectangle.BottomBorder);
		bool collideWithRightBorder = CollisionHelper.CheckTwoSegmentCrose(movementVector2, targetRectangle.RightBorder);
		bool collideWithTopBorder = CollisionHelper.CheckTwoSegmentCrose(movementVector2, targetRectangle.TopBorder);
		
		
		return new Vector2(0, 0);			
	}*/

        /// <summary>
        /// Gets the position of the center of the bottom edge of the rectangle.
        /// </summary>
        
	public static Vector2 GetBottomCenter(this Rectangle rect)
    {
        return new Vector2(rect.X + rect.Width / 2.0f, rect.Bottom);
    }
    
}
